#include "Precompile.h"
#include "DX11PixelShader.h"
#include "DX11RenderSystem.h"

namespace Graphics{
	DX11PixelShader::DX11PixelShader(std::string &filename, RenderContext* handle):PixelShader(filename,handle){
		DX11RenderContext* ctx = (DX11RenderContext*)handle;

		std::string shaderModel;

		switch(static_cast<DX11RenderSystem*>(ctx->m_renderSystem)->getFeatureLevel()){
		case D3D_FEATURE_LEVEL_9_1:
		case D3D_FEATURE_LEVEL_9_2:
			LOG_ERROR("Unsupported shader model");
			assert(0);
			return;
		case D3D_FEATURE_LEVEL_9_3:
			shaderModel = "ps_3_0";
			break;
		case D3D_FEATURE_LEVEL_10_0:
		case D3D_FEATURE_LEVEL_10_1:
			shaderModel = "ps_4_0";
			break;
		case D3D_FEATURE_LEVEL_11_0:
			shaderModel = "ps_5_0";
			break;
		}

		void* byteCode;
		size_t length;
		char buffer[10000];
		std::ifstream file(filename,std::ios::binary);
		file.read((char*)buffer,10000);
		byteCode = buffer;
		length = size_t(file.gcount());
		
		HRESULT hr;
		hr = ctx->m_handle->CreatePixelShader((DWORD*)byteCode,length,NULL,&m_shader);
		if(FAILED(hr)){
			LOG_ERROR("DX11PixelShader::DX11PixelShader - Failed in creating pixel shader.");
			assert(0);
		}

		file.close();
	}

	DX11PixelShader::~DX11PixelShader(){
		if(m_shader)
			m_shader->Release();
	}

	void DX11PixelShader::setShader(void* context){
		DX11RenderContext* ctx = (DX11RenderContext*)context;
		ctx->m_context->PSSetShader(m_shader,NULL,0);
	}
}